package com.itheima.jobs;


import com.itheima.constant.RedisConstant;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisPool;

import java.util.Iterator;
import java.util.Set;

@Component("delQiNiuPic")
public class DelQiNiuPic {

    @Autowired
    private JedisPool jedisPool;

    public void delImage(){
        /*
        //上传的文件
        Set<String> upload = jedisPool.getResource().smembers(RedisConstant.SETMEAL_PIC_RESOURCES);

        //添加到数据库的有效文件
        Set<String> db = jedisPool.getResource().smembers(RedisConstant.SETMEAL_PIC_DB_RESOURCES);


        for (String str : db) {
            upload.remove(str);
        }
        */

        //redis中可以直接取两个集合的差值
        Set<String> sdiff = jedisPool.getResource().sdiff(RedisConstant.SETMEAL_PIC_RESOURCES,
                        RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        //遍历移除后，upload集合中是要删除的文件名
        for (String delStr : sdiff) {
            //调用utils，删除七牛云服务器上的无效图片
            QiniuUtils.deleteFileFromQiniu(delStr);
            System.out.println(delStr+"图片已经删除成功");

            //同时，将redis数据库中的upload数据更新
            //即将无效的数据从上传图片的集合中移除
            jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES,delStr);
        }


    }
}
